package com.thekernel.youdaoyun.controller;

import com.alibaba.fastjson.JSONObject;
import com.thekernel.youdaoyun.annotation.Log;
import com.thekernel.youdaoyun.bean.Feedback;
import com.thekernel.youdaoyun.bean.Note;
import com.thekernel.youdaoyun.exception.MyException;
import com.thekernel.youdaoyun.exception.impl.DataAlreadyExistsException;
import com.thekernel.youdaoyun.exception.impl.DataNotFoundException;
import com.thekernel.youdaoyun.exception.impl.SystemErrorException;
import com.thekernel.youdaoyun.service.FeedbackService;
import com.thekernel.youdaoyun.util.ResponseUtils;
import com.thekernel.youdaoyun.util.UserUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: thekernel
 * @Date: 2020/3/25 22:08
 * @Version: 1.0
 * @Description:
 */
@RestController
@RequestMapping("/feedback")
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    /**
     * 添加反馈 API
     *
     * @param data 反馈内容, 类型等
     * @return String 是否成功以及信息
     */
//    @Log("feedback_add")
    @ApiOperation(value = "添加反馈", notes = "用户提交反馈")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content", value = "反馈内容", required = true, dataType = "string"),
            @ApiImplicitParam(name = "type", value = "反馈类型(0: 反馈 1: 投诉)", dataType = "int"),
    })
    @PostMapping("add")
    public @ResponseBody
    String addNote(@RequestBody JSONObject data) throws MyException {
        Feedback feedback = data.toJavaObject(Feedback.class);
        feedback.setUserId(UserUtils.checkReturnUserId());
        int result = this.feedbackService.addFeedback(feedback);

        if (result == 1) {
            JSONObject returnData = new JSONObject();
            returnData.put("feedbackId", feedback.getId());
            return ResponseUtils.response(ResponseUtils.SUCCESS, "Feedback Add Success", returnData);
        } else if (result == 0) {
            throw new DataAlreadyExistsException();
        } else if (result == -1) {
            throw new DataNotFoundException();
        } else {
            throw new SystemErrorException();
        }
    }

}
